﻿using System;
using System.Collections.Generic;
using System.Text;
using TTClassLibrary;
using TTServer.Messages;
using libCommonClass;
using TTServer.Controller;
using libInterface;
using libUtilities;
using System.Drawing;
using bmttPPTController.TTServer;

namespace TTServer.MessageProcessor
{
    public class PPTActivateWindowRequestMessageProcessor : IMessageProcessor
    {
        public bool Process(libCommonClass.TTMessage message, libInterface.IClientInfoProvider clientInfoProvider)
        {
            if (message is PPTActivateWindowRequestMessage)
            {
                PPTActivateWindowRequestMessage msg = (PPTActivateWindowRequestMessage)message;

                if (!(clientInfoProvider.Authority == AuthorityConstant.ADMIN
                    || clientInfoProvider.Authority == AuthorityConstant.KEY_MEMBER))
                {
                    return true;
                }

                PPTController pptCtrl = PPTController.Instance;

                if (!pptCtrl.checkPPTApplicationRun())
                {
                    NotifyMessage nm = new NotifyMessage("PPT Controller not run");

                    clientInfoProvider.AddToMessageQueue(nm);

                    return true;
                }

                int re = PPTController.Instance.ActivateWindow(msg.Index_0b, msg.PresentationName);

                if (re == 0)
                {
                    NotifyMessage t_msg = new NotifyMessage("Invalid Operation");

                    clientInfoProvider.AddToMessageQueue(t_msg);
                }
                else
                {
                    String[] names = null;
                    int activeWindowIndex = -1;

                    int re1 = PPTController.Instance.GetWindowRelationship(ref names, ref activeWindowIndex);

                    if (re1 == 1)
                    {
                        PPTWindowsRelationshipResponse wrr = new PPTWindowsRelationshipResponse();
                        wrr.TemporaryKey = clientInfoProvider.TemporaryKey;
                        wrr.PresentationNames = names;
                        wrr.ActiveWindowIndex = activeWindowIndex;

                        clientInfoProvider.AddToMessageQueue(wrr);
                    }
                    else
                    {
                        // Thong bao la khong thuc hien duoc thao tac nay
                        NotifyMessage nm = new NotifyMessage("Fetch data unsuccessfully!!!");

                        clientInfoProvider.AddToMessageQueue(nm);
                    }
                }

                return true;
            }

            return false;
        }
    }
}
